package com.pxxy.lawconsult.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.pxxy.lawconsult.bean.ClientUser;
import com.pxxy.lawconsult.bean.RegisterData;
import com.pxxy.lawconsult.bean.User;
import com.pxxy.lawconsult.constant.DBConstant;
import com.pxxy.lawconsult.utils.DBUtils;
import com.pxxy.lawconsult.utils.SetBeanUtils;

@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private Gson gson;
	private RegisterData data;

	public RegisterServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String tel = request.getParameter(User.TEL);
		String password = request.getParameter(User.PASSWORD);
		
		gson = new Gson();
		data = new RegisterData();
		PrintWriter out = response.getWriter();

		if (tel == null || tel.equals("")) {
			data.setRegisterState(RegisterData.REGISTERSTATE_FAILURE);
			out.write(gson.toJson(data));
			return;
		} else if (password == null || password.equals("")) {
			data.setRegisterState(RegisterData.REGISTERSTATE_FAILURE);
			out.write(gson.toJson(data));
			return;
		}

		register(tel, password, out);

	}

	/**
	 * 注册用户
	 * 
	 * @param tel
	 * @param password
	 * @param out
	 */
	public void register(String tel, String password, PrintWriter out) {
		try {
			// 先查询用户是否存在
			ResultSet resultSet = DBUtils.query(DBConstant.TB_USER_TABLE, DBConstant.USER_TABLE_TEL, tel);
			if (resultSet.next()) {
				data.setRegisterState(RegisterData.REGISTERSTATE_FAILURE_USEREXIST);
				return;
			}
			// User表插入数据
			int result = DBUtils.insert(DBConstant.TB_USER_TABLE,
					new String[] { DBConstant.USER_TABLE_TEL, DBConstant.USER_TABLE_PASSWORD,
							DBConstant.USER_TABLE_IMAGE },
					new String[] { tel, password, "http://120.77.205.137/photo.jpg" });
			String userId;
			if (result == 1) {
				// client表插入数据
				ResultSet rs = DBUtils.query(DBConstant.TB_USER_TABLE, DBConstant.USER_TABLE_TEL, tel);
				if (rs.next()) {
					// 查询userId
					userId = rs.getInt(DBConstant.USER_TABLE_ID) + "";
					// 向client插入数据
					int result2 = DBUtils.insert(
							DBConstant.TB_CLIENT_TABLE, new String[] { DBConstant.CLIENT_TABLE_NICKNAME,
									DBConstant.CLIENT_TABLE_SIGNATURE, DBConstant.CLIENT_TABLE_USER_ID },
							new String[] { tel, "这个人很懒，什么都没留下!", userId });

					// 判断是否成功
					if (result2 == 1) {
						ResultSet userResult = DBUtils.query(DBConstant.TB_USER_TABLE, DBConstant.USER_TABLE_TEL, tel);
						ClientUser user = null;
						if (userResult.next()) {
							user = SetBeanUtils.loginClientUser(userResult);
						}
						// 返回注册成功
						data.setRegisterState(RegisterData.REGISTERSTATE_SUCCESS);
						// 返回用户数据
						data.setData(user);
					}
				}
			} else {
				// 返回注册失败
				data.setRegisterState(RegisterData.REGISTERSTATE_FAILURE);
			}
		} catch (Exception e) {
			data.setRegisterState(RegisterData.REGISTERSTATE_FAILURE_SERVER_ERROR);
			e.printStackTrace();
		} finally {
			// 写出数据
			String registerJson = gson.toJson(data);
			out.println(registerJson);
			out.close();
			// 关闭数据库连接
			DBUtils.close();
		}
	}

}
